package model;

import java.lang.Thread.State;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author pyin
 */
public class TSRunner {

    private static Queue queuedJobs = new LinkedList();
    private static List runningJobs = new LinkedList();
    private static TSRunnnerThread runnerThread = new TSRunnnerThread();
    private static boolean run;
    

    static {
        runnerThread.start();
        stop();
    }

    public static TSRunnnerThread getRunnerThead() {
        return runnerThread;
    }

    public static void addJob(TestScript ts) {
        queuedJobs.add(ts);
    }



    public static Queue getQueuedJobs() {
        return queuedJobs;
    }

    public static List getRunningJobs() {
        return runningJobs;
    }

    public static void start() {
        resume();
    }

    public static void stop() {
        pause();
    }

    public static void pause() {
        synchronized (runnerThread) {
            runnerThread.pause = true;
        }
    }

    public static void resume() {
        synchronized (runnerThread) {
            runnerThread.pause = false;
            runnerThread.notify();
        }
    }

    public static boolean isRunning() {
        State state = runnerThread.getState();
        if (state == State.WAITING) {
            return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) {
//        TSRunner.start();
        if (!runnerThread.pause) {
            System.out.println("running");

        } else {
            System.out.println("stopped");
        }
    }

    static public class TSRunnnerThread extends Thread {

        boolean pause = false;
        private TSRunner parent;

        public TSRunnnerThread() {
            pause = false;
//            run();
        }

        public void run(){
            while (true) {
                try {
                    synchronized (this) {
                        while (pause) {
                            this.wait();
                        }
                        if (runningJobs.size() > 0) {
                            Iterator it = runningJobs.iterator();
                            while (it.hasNext()) {
                                TestScript ts = (TestScript) it.next();
                                if (ts.getState() == TestScript.STATE_COMPLETED_OK) {
                                    runningJobs.remove(ts);
                                }
                            }
                        }
                        if (!queuedJobs.isEmpty()) {
                            TestScript script = (TestScript) queuedJobs.peek();
                            if (!ifJobRunningOnMachine(script.getMachine())) {
                                script = (TestScript) queuedJobs.poll();
                                runningJobs.add(script);
                                script.run();
                            }
                        }
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(TSRunner.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        private boolean ifJobRunningOnMachine(String machine) {
            Iterator it = getRunningJobs().iterator();
            while (it.hasNext()) {
                TestScript ts = (TestScript) it.next();
                if (ts.getMachine().equals(machine)) {
                    return true;
                }
            }
            return false;
        }
    }
}
